package xhwl.hmo.mall.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.trello.rxlifecycle.components.support.RxFragmentActivity;

import java.util.ArrayList;
import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.adapter.BasicsAdapter;
import xhwl.hmo.mall.http.api.CardStatusApi;
import xhwl.hmo.mall.http.bean.resp.CardStatusResp;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.hmo.mall.utils.ToastUtil;
import xhwl.hmo.mall.views.HeaderGridView;
import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class BasicsFragment extends BaseFragment {
    private String cardStatus;// 3=健康套装 2=重疾卡 1=HMO卡
    ImageView img_cardkind;
    List<CardStatusResp> cardStatuslist =new ArrayList<CardStatusResp>();
    BasicsAdapter basicsAdapter;
    HeaderGridView gv_basic;
    RefreshLayout refreshLayout;
    int page_num = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cardStatus=getArguments().getString("cardStatus");

    }
    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_basics, container, false);
    }

    @Override
    protected void initView(View view) {
        refreshLayout = (RefreshLayout) view.findViewById(R.id.refreshLayout_basics);
        gv_basic= (HeaderGridView) view.findViewById(R.id.gv_basics);
        View headerView=LayoutInflater.from(mActivity).inflate(R.layout.basic_header,null);
        img_cardkind = (ImageView) headerView.findViewById(R.id.img_cardkind);
        if("1".equals(cardStatus)){
            img_cardkind.setImageDrawable(getResources().getDrawable(R.drawable.basicscard));
        }else if("2".equals(cardStatus)){
            img_cardkind.setImageDrawable(getResources().getDrawable(R.drawable.illnesscard));
        }else if("3".equals(cardStatus)){
            img_cardkind.setImageDrawable(getResources().getDrawable(R.drawable.healthsuit));
        }
        gv_basic.addHeaderView(headerView);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                requestcard(page_num + 1);
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                requestcard(1);
            }
        });
        basicsAdapter = new BasicsAdapter(getActivity(), cardStatuslist, cardStatus);
        gv_basic.setAdapter(basicsAdapter);

    }

    @Override
    protected void initListener() {

    }
    @Override
    protected void loadData() {
        requestcard(1);

    }
    public void requestcard(final int page){
        CardStatusApi cardStatusApi=new CardStatusApi(cardStatus,"",page);
        HttpManager.getInstance().dealHttp((RxFragmentActivity) mActivity, cardStatusApi, new OnNextListener<HttpResp<ArrayList<CardStatusResp>>>() {
            @Override
            public void onNext(HttpResp<ArrayList<CardStatusResp>> cardstatusResp) {

                if (page > 1) {
                    refreshLayout.finishLoadmore();
                }else
                {
                    refreshLayout.finishRefresh();
                }
                if (cardstatusResp.data.size() > 0) {
                    page_num = page;
                    if(page==1)
                    {
                        cardStatuslist.clear();
                    }
                    cardStatuslist.addAll(cardstatusResp.data);
                    basicsAdapter.notifyDataSetChanged();
                } else {
                    ToastUtil.showToast(mActivity, getString(R.string.no_more_data));
//                        refreshLayout.setEnableLoadmore(false);
                }


//                for (int i=0;i<cardstatusResp.data.size();i++){
//                    cardStatuslist.add(cardstatusResp.data.get(i));
//                }



            }
            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
                if (page > 1) {
                    refreshLayout.finishLoadmore();
                }else
                {
                    refreshLayout.finishRefresh();
                }
            }
        });

    }

}
